package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SysActiveRule;
import com.ruoyi.system.mapper.SysActiveMapper;
import com.ruoyi.system.domain.SysActive;
import com.ruoyi.system.service.ISysActiveService;

/**
 * 抽奖活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-30
 */
@Service
public class SysActiveServiceImpl implements ISysActiveService 
{
    @Autowired
    private SysActiveMapper sysActiveMapper;

    /**
     * 查询抽奖活动
     * 
     * @param activeId 抽奖活动主键
     * @return 抽奖活动
     */
    @Override
    public SysActive selectSysActiveByActiveId(Long activeId)
    {
        return sysActiveMapper.selectSysActiveByActiveId(activeId);
    }

    /**
     * 查询抽奖活动列表
     * 
     * @param sysActive 抽奖活动
     * @return 抽奖活动
     */
    @Override
    public List<SysActive> selectSysActiveList(SysActive sysActive)
    {
        return sysActiveMapper.selectSysActiveList(sysActive);
    }

    /**
     * 新增抽奖活动
     * 
     * @param sysActive 抽奖活动
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysActive(SysActive sysActive)
    {
        sysActive.setCreateTime(DateUtils.getNowDate());
        int rows = sysActiveMapper.insertSysActive(sysActive);
        insertSysActiveRule(sysActive);
        return rows;
    }

    /**
     * 修改抽奖活动
     * 
     * @param sysActive 抽奖活动
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysActive(SysActive sysActive)
    {
        sysActive.setUpdateTime(DateUtils.getNowDate());
        sysActiveMapper.deleteSysActiveRuleByActiveId(sysActive.getActiveId());
        insertSysActiveRule(sysActive);
        return sysActiveMapper.updateSysActive(sysActive);
    }

    /**
     * 批量删除抽奖活动
     * 
     * @param activeIds 需要删除的抽奖活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysActiveByActiveIds(Long[] activeIds)
    {
        sysActiveMapper.deleteSysActiveRuleByActiveIds(activeIds);
        return sysActiveMapper.deleteSysActiveByActiveIds(activeIds);
    }

    /**
     * 删除抽奖活动信息
     * 
     * @param activeId 抽奖活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysActiveByActiveId(Long activeId)
    {
        sysActiveMapper.deleteSysActiveRuleByActiveId(activeId);
        return sysActiveMapper.deleteSysActiveByActiveId(activeId);
    }

    /**
     * 新增活动规则信息
     * 
     * @param sysActive 抽奖活动对象
     */
    public void insertSysActiveRule(SysActive sysActive)
    {
        List<SysActiveRule> sysActiveRuleList = sysActive.getSysActiveRuleList();
        Long activeId = sysActive.getActiveId();
        if (StringUtils.isNotNull(sysActiveRuleList))
        {
            List<SysActiveRule> list = new ArrayList<SysActiveRule>();
            for (SysActiveRule sysActiveRule : sysActiveRuleList)
            {
                sysActiveRule.setActiveId(activeId);
                list.add(sysActiveRule);
            }
            if (list.size() > 0)
            {
                sysActiveMapper.batchSysActiveRule(list);
            }
        }
    }
}
